#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
#define maxn 22222
#define INF 0x3f3f3f3f
int dp[22][maxn], n, v[222], p[222], c, m, cmin, cmax;
int main()
{
    while (~scanf("%d", &n))
    {
        memset(dp, INF, sizeof(dp));
        for (int i = 0; i < n; i++)
            scanf("%d%d", &v[i], &p[i]);
        dp[0][0] = 0;
        for (int i = 0; i < n; i++)
            for (int k = 20000; k >= v[i]; k--)
            {
                int tn = min(n, 20);
                for (int j = 1; j <= tn; j++)
                    if (dp[j - 1][k - v[i]] != INF)
                        dp[j][k] = min(dp[j][k], dp[j - 1][k - v[i]] + p[i]);
            }
        scanf("%d", &c);
        while (c--)
        {
            scanf("%d%d%d", &m, &cmin, &cmax);
            if (m > n)
            {
                printf("impossible\n");
                continue;
            }
            int ans = INF;
            for (int i = m * cmin; i <= m * cmax; i++)
                ans = min(ans, dp[m][i]);
            if (ans != INF)
                printf("%d\n", ans);
            else
                printf("impossible\n");
        }
    }
    return 0;
}